package com.xj.reggie.service.impl;

import com.xj.reggie.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

@Service
public class SendMailServiceImpl implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;

    //发件人
    private String from = "1605059891@qq.com";


    //邮件标题
    private String subject = "瑞吉外卖登录验证";

    //邮件正文
    private String text = "<img src='https://i02piccdn.sogoucdn.com/00a520aa7d374056'/><br><a href='http://localhost:8080/front/page/login.html'>去登录</a>";

    @Override
    public void sendMail(String to,String code) {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from+ "(瑞吉外卖)");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text+"你的验证码为==>"+code+"<==请妥善保管^_^", true);

            //附件发送
//            File xlsxFile = new File("D:\\桌面\\零食工坊注册须知.pdf");
//            helper.addAttachment(xlsxFile.getName(), xlsxFile);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        javaMailSender.send(message);
    }
}
